<script setup>
import { ref, reactive, onMounted } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'

// 搜索表单
const searchForm = reactive({
  name: '',
  status: ''
})

// 景点列表数据
const attractionList = ref([])
const loading = ref(false)

// 分页配置
const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 0
})

// 表格列配置
const columns = [
  { colKey: 'id', title: 'ID', width: 80 },
  { colKey: 'name', title: '景点名称' },
  { colKey: 'location', title: '所在地' },
  { colKey: 'price', title: '门票价格' },
  { colKey: 'rating', title: '评分' },
  { 
    colKey: 'status', 
    title: '状态',
    cell: (h, { row }) => {
      return h('t-tag', {
        theme: row.status === 'active' ? 'success' : 'default',
        variant: 'light'
      }, row.status === 'active' ? '上架' : '下架')
    }
  },
  { colKey: 'updateTime', title: '更新时间' },
  { colKey: 'operation', title: '操作', width: 200 }
]

// 获取景点列表
const fetchAttractionList = async () => {
  loading.value = true
  try {
    // 模拟数据
    const mockData = Array.from({ length: 86 }, (_, i) => ({
      id: i + 1,
      name: `景点${i + 1}`,
      location: ['贵阳市', '遵义市', '安顺市', '毕节市'][Math.floor(Math.random() * 4)],
      price: Math.floor(Math.random() * 200) + 50,
      rating: (Math.random() * 2 + 3).toFixed(1),
      status: Math.random() > 0.2 ? 'active' : 'inactive',
      updateTime: new Date().toLocaleDateString()
    }))
    
    // 模拟分页
    const start = (pagination.current - 1) * pagination.pageSize
    const end = start + pagination.pageSize
    attractionList.value = mockData.slice(start, end)
    pagination.total = mockData.length
  } catch (error) {
    MessagePlugin.error('获取景点列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.current = 1
  fetchAttractionList()
}

// 重置
const handleReset = () => {
  searchForm.name = ''
  searchForm.status = ''
  handleSearch()
}

// 分页变化
const handlePageChange = (pageInfo) => {
  pagination.current = pageInfo.current
  pagination.pageSize = pageInfo.pageSize
  fetchAttractionList()
}

// 编辑景点
const handleEdit = (row) => {
  MessagePlugin.info(`编辑景点: ${row.name}`)
}

// 删除景点
const handleDelete = (row) => {
  MessagePlugin.warning(`删除景点: ${row.name}`)
}

// 上下架
const handleToggleStatus = (row) => {
  const action = row.status === 'active' ? '下架' : '上架'
  MessagePlugin.success(`${action}景点: ${row.name}`)
}

onMounted(() => {
  fetchAttractionList()
})
</script>

<template>
  <div class="attraction-list-container">
    <!-- 搜索区域 -->
    <t-card class="search-card">
      <t-form :data="searchForm" layout="inline">
        <t-form-item label="景点名称">
          <t-input v-model="searchForm.name" placeholder="请输入景点名称" clearable />
        </t-form-item>
        <t-form-item label="状态">
          <t-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <t-option value="active" label="上架" />
            <t-option value="inactive" label="下架" />
          </t-select>
        </t-form-item>
        <t-form-item>
          <t-space>
            <t-button theme="primary" @click="handleSearch">查询</t-button>
            <t-button theme="default" @click="handleReset">重置</t-button>
          </t-space>
        </t-form-item>
      </t-form>
    </t-card>

    <!-- 操作区域 -->
    <t-card class="table-card">
      <div class="table-header">
        <h3>景点列表</h3>
        <t-button theme="primary">
          <template #icon><t-icon name="add" /></template>
          新增景点
        </t-button>
      </div>

      <!-- 表格 -->
      <t-table
        :data="attractionList"
        :columns="columns"
        :loading="loading"
        :pagination="pagination"
        @page-change="handlePageChange"
        stripe
        hover
      >
        <template #operation="{ row }">
          <t-space>
            <t-link theme="primary" hover="color" @click="handleEdit(row)">编辑</t-link>
            <t-link 
              :theme="row.status === 'active' ? 'warning' : 'success'" 
              hover="color" 
              @click="handleToggleStatus(row)"
            >
              {{ row.status === 'active' ? '下架' : '上架' }}
            </t-link>
            <t-link theme="danger" hover="color" @click="handleDelete(row)">删除</t-link>
          </t-space>
        </template>
      </t-table>
    </t-card>
  </div>
</template>

<style scoped>
.attraction-list-container {
  height: 100%;
}

.search-card {
  margin-bottom: 16px;
}

.table-card {
  height: calc(100% - 100px);
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.table-header h3 {
  margin: 0;
}
</style> 